<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use DateTimeInterface;

/**
 * 报溢报损详细
 * Class OverflowBreakageDetailModel
 * @package App\Models
 */
class OverflowBreakageDetailModel extends Model
{
    protected $table = 'overflow_breakage_detail';

    protected $dateFormat = 'U';//这个时间字段被为被格式化为UNIX 时间戳的形式存储
    //toArray 时间转换问题
    protected function serializeDate(DateTimeInterface $date)
    {
        return $date->format('Y-m-d H:i:s');
    }

    static public function getMainId($keyword, $data)
    {
        return SELF::select(
            'o_b_main_id'
        )
            ->from('overflow_breakage_detail as a')
            ->leftJoin('materiel_type as b', 'b.id', '=', 'a.materiel_type_id')
            ->leftJoin('storage as c', 'c.id', '=', 'a.warehouse_id')
            ->leftJoin('overflow_breakage as d', 'd.id', '=', 'a.o_b_main_id')
            ->where('a.main_id', $data['main_id'])
            ->where([
                'd.repeal_status' => 0,
                'd.type' => $data['type']
            ])
            ->orWhere(function($query) use ($keyword) {
                $query->orWhere('b.materiel_name', 'like', '%'.$keyword.'%');
                $query->orWhere('b.type_name', 'like', '%'.$keyword.'%');
                $query->orWhere('b.materiel_no', 'like', '%'.$keyword.'%');
            })
            ->distinct()
            ->get()->toArray();
    }
}
